package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class ConfigPanel extends JPanel {
    private JTextArea headerTextArea;
    private JCheckBox unauthorizedCheck;
    private JCheckBox ignore204304Check;
    private JCheckBox avoid304Check;
    private JCheckBox useConfigRulesCheck;
    private RulePanel lowPrivilegeRulePanel;
    private RulePanel unauthorizedRulePanel;
    private InterceptFilterPanel interceptFilterRulePanel;

    private ActionListener startTestActionListener;
    private ActionListener clearListActionListener;

    public ConfigPanel() {
        initializeComponents();
    }

    private void initializeComponents() {
        setLayout(new BorderLayout());

        // 创建规则标签面板
        JTabbedPane ruleTabbedPane = new JTabbedPane();

        // 清空列表按钮
        JPanel clearPanel = new JPanel();
        clearPanel.setLayout(new BoxLayout(clearPanel, BoxLayout.Y_AXIS));
        JButton clearButton = getClearButton();

        // 开始测试按钮
        JButton startTestButton = new JButton("开始测试");
        startTestButton.setAlignmentX(Component.LEFT_ALIGNMENT);
        startTestButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        startTestButton.addActionListener(e -> {
            if (startTestActionListener != null) {
                startTestActionListener.actionPerformed(e);
            }
        });

        // 临时请求头输入框
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        JLabel headerLabel = new JLabel("临时请求头");
        headerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        headerLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        headerTextArea = new JTextArea(3, 20);
        headerTextArea.setLineWrap(true);
        headerTextArea.setWrapStyleWord(true);
        headerTextArea.setText("Cookie: Insert=injected; cookie=or;\nHeader: here");
        JScrollPane scrollPane = new JScrollPane(headerTextArea);
        scrollPane.setPreferredSize(new Dimension(300, 60));
        scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);

        // 添加"检查未授权"复选框，默认勾选
        unauthorizedCheck = new JCheckBox("检查未授权", true);
        unauthorizedCheck.setAlignmentX(Component.LEFT_ALIGNMENT);
        unauthorizedCheck.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // 添加"忽略请求响应码204|304"复选框，默认勾选
        ignore204304Check = new JCheckBox("忽略请求响应码204|304", true);
        ignore204304Check.setAlignmentX(Component.LEFT_ALIGNMENT);
        ignore204304Check.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // 添加"避免服务器返回304响应"复选框，默认不勾选
        avoid304Check = new JCheckBox("避免服务器返回304响应", false);
        avoid304Check.setAlignmentX(Component.LEFT_ALIGNMENT);
        avoid304Check.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // 添加"使用配置规则"复选框，默认勾选
        useConfigRulesCheck = new JCheckBox("使用配置规则", true);
        useConfigRulesCheck.setAlignmentX(Component.LEFT_ALIGNMENT);
        useConfigRulesCheck.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // 将组件添加到内部面板中
        contentPanel.add(clearButton);
        contentPanel.add(Box.createVerticalStrut(10));
        contentPanel.add(startTestButton);
        contentPanel.add(Box.createVerticalStrut(10));
        contentPanel.add(headerLabel);
        contentPanel.add(scrollPane);
        contentPanel.add(unauthorizedCheck);
        contentPanel.add(ignore204304Check);
        contentPanel.add(avoid304Check);
        contentPanel.add(useConfigRulesCheck);

        // 将内容面板添加到主面板的上方
        add(contentPanel, BorderLayout.NORTH);

        // 添加标签页
        lowPrivilegeRulePanel = new RulePanel();
        unauthorizedRulePanel = new RulePanel();
        interceptFilterRulePanel = new InterceptFilterPanel();
        ruleTabbedPane.addTab("低权限防御规则", lowPrivilegeRulePanel);
        ruleTabbedPane.addTab("未授权防御规则", unauthorizedRulePanel);
        ruleTabbedPane.addTab("拦截过滤器", interceptFilterRulePanel);
        add(ruleTabbedPane, BorderLayout.CENTER);
    }

    private JButton getClearButton() {
        JButton clearButton = new JButton("清空列表");
        clearButton.setAlignmentX(Component.LEFT_ALIGNMENT);
        clearButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        clearButton.addActionListener(e -> {
            if (clearListActionListener != null) {
                clearListActionListener.actionPerformed(e);
            }
        });
        return clearButton;
    }

    // Getter方法
    public String getTempHeaders() {
        return headerTextArea.getText();
    }

    public boolean isUnauthorizedCheckSelected() {
        return unauthorizedCheck.isSelected();
    }

    public boolean isIgnore204304Checked() {
        return ignore204304Check.isSelected();
    }

    public boolean isAvoid304Checked() {
        return avoid304Check.isSelected();
    }

    public boolean isUseConfigRulesChecked() {
        return useConfigRulesCheck.isSelected();
    }

    public RulePanel getLowPrivilegeRulePanel() {
        return lowPrivilegeRulePanel;
    }

    public RulePanel getUnauthorizedRulePanel() {
        return unauthorizedRulePanel;
    }

    public InterceptFilterPanel getInterceptFilterRulePanel() {
        return interceptFilterRulePanel;
    }

    // Setter方法
    public void setStartTestActionListener(ActionListener listener) {
        this.startTestActionListener = listener;
    }

    public void setClearListActionListener(ActionListener listener) {
        this.clearListActionListener = listener;
    }

    // 添加清空临时请求头内容的方法
    public void clearTempHeaderContent() {
        if (headerTextArea != null) {
            headerTextArea.setText("");
        }
    }

    // 添加设置临时请求头内容的方法
    public void setTempHeaderContent(String content) {
        if (headerTextArea != null) {
            headerTextArea.setText(content);
        }
    }

    // 添加设置复选框状态的方法
    public void setUnauthorizedCheck(boolean selected) {
        unauthorizedCheck.setSelected(selected);
    }

    public void setIgnore204304Check(boolean selected) {
        ignore204304Check.setSelected(selected);
    }

    public void setAvoid304Check(boolean selected) {
        avoid304Check.setSelected(selected);
    }

    public void setUseConfigRulesCheck(boolean selected) {
        useConfigRulesCheck.setSelected(selected);
    }
}
